package com.qding.park.manager.service.core.dao.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.manager.pojo.ap.ApAppearanceRecord;
import com.qding.park.manager.pojo.enums.OrderSourceEnum;
import com.qding.park.manager.pojo.noncore.NcMonthRent;
import com.qding.park.manager.pojo.noncore.NcParkInoutInfo;
import com.qding.park.manager.pojo.noncore.NcTempPayInfo;
import com.qding.park.manager.service.core.dao.MongodbDao;

@Repository("MongodbDao")
public class MongodbDaoImpl implements MongodbDao {

	private static final Log _log = LogFactory.getLog(MongodbDaoImpl.class);
	/**
	 * @Fields mongoTemplate : TODO（mongodb操作模板）
	 */
	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void saveIncheck(NcParkInoutInfo ncParkInoutInfo) {
		// TODO Auto-generated method stub
		// //进出场表名
		String checkinout_tablename = MongodbConstant.TABLENAME_CHECKINOUT;
		// Document doc=BsonUtil.toBson(ncParkInoutInfo);
		mongoTemplate.insert(ncParkInoutInfo, checkinout_tablename);
	}

	@Override
	public void saveOutcheck(NcParkInoutInfo ncParkInoutInfo) {
		int parkId = ncParkInoutInfo.getParkId();
		String enInID = ncParkInoutInfo.getEnInID();
		String checkout_tablename = MongodbConstant.TABLENAME_CHECKOUT;
		// _log.info("非重复的出场信息");

		String enOutID = ncParkInoutInfo.getEnOutID();
		String checkinout_tablename = MongodbConstant.TABLENAME_CHECKINOUT;

		Criteria criteria = new Criteria();
		criteria.and("parkId").is(parkId);
		criteria.and("enInID").is(enInID);
		Query query = new Query();
		query.addCriteria(criteria);
		_log.info(criteria.getCriteriaObject());
		Update update = new Update();
		int shouldFeeMoney=0;
		int feeMoney=0;
//		int orderSource=0;
		ncParkInoutInfo.setOrderSource(OrderSourceEnum.OUTLINE.getType());
		if (ncParkInoutInfo.getShouldFeeMoney() == 0 || ncParkInoutInfo.getFeeMoney() == 0) {
			Criteria criteria_1 = new Criteria();
			criteria_1.and("enInID").is(ncParkInoutInfo.getEnInID());
			criteria_1.and("supplyID").is(ncParkInoutInfo.getSupplyID());
			Query query_1 = new Query();
			query_1.addCriteria(criteria_1);
			NcTempPayInfo  tempPay=mongoTemplate.findOne(query_1, NcTempPayInfo.class, MongodbConstant.TABLENAME_TEMPPAY);
			if(tempPay!=null){
				shouldFeeMoney=tempPay.getShouldFeeMoney();
				feeMoney=tempPay.getFeeMoney();
				ncParkInoutInfo.setOrderSource(tempPay.getOrderSource());
			}
		}
		update.set("enOutID", enOutID);
		update.set("orderSource", ncParkInoutInfo.getOrderSource());
		update.set("outTime", ncParkInoutInfo.getOutTime());
		update.set("outRoomName", ncParkInoutInfo.getOutRoomName());
		update.set("opType", ncParkInoutInfo.getOpType());
		update.set("opSpecial", ncParkInoutInfo.getOpSpecial());
		update.set("cardType", ncParkInoutInfo.getCardType());
		update.set("shouldFeeMoney", shouldFeeMoney==0?ncParkInoutInfo.getShouldFeeMoney():shouldFeeMoney);
		update.set("feeMoney", feeMoney==0?ncParkInoutInfo.getFeeMoney():feeMoney);
		update.set("update_time", ncParkInoutInfo.getUpdate_time());
		update.set("carType", ncParkInoutInfo.getCarType());
		update.set("update_time", new Date());
		update.set("operator", ncParkInoutInfo.getOperator());
		update.set("parkInPic", ncParkInoutInfo.getParkInPic());
		update.set("parkOutPic", ncParkInoutInfo.getParkOutPic());

		mongoTemplate.updateMulti(query, update, NcParkInoutInfo.class, checkinout_tablename);
		/**********/
		NcParkInoutInfo  ncClone=ncParkInoutInfo.clone();
		ncClone.setFeeMoney(feeMoney==0?ncParkInoutInfo.getFeeMoney():feeMoney);
		ncClone.setShouldFeeMoney(shouldFeeMoney==0?ncParkInoutInfo.getShouldFeeMoney():shouldFeeMoney);
		mongoTemplate.insert(ncClone, checkout_tablename);
	}

	@Override
	public NcParkInoutInfo queryIncheckByCarnum(String carNum) {
		// TODO Auto-generated method stub
		Criteria criteria = new Criteria();
		// criteria.where("carNum").is(carNum);
		criteria.and("carNum").is(carNum);
		Query query = new Query();
		query.addCriteria(criteria);

		query.with(new Sort(Direction.DESC, "enterTime"));
		query.limit(1);
		NcParkInoutInfo ncParkInoutInfo = mongoTemplate.findOne(query, NcParkInoutInfo.class,
				MongodbConstant.TABLENAME_CHECKINOUT);
		return ncParkInoutInfo;
	}

	@Override
	public void saveNcMonthRent(NcMonthRent ncMonthRent) {
		String checkinout_tablename = MongodbConstant.TABLENAME_MONTHRENT;
		mongoTemplate.save(ncMonthRent, checkinout_tablename);
	}

	@Override
	public void saveNcTempPayInfo(NcTempPayInfo ncTempPayInfo) {
		String checkinout_tempPay = MongodbConstant.TABLENAME_TEMPPAY;
		mongoTemplate.save(ncTempPayInfo, checkinout_tempPay);
		
		
		Criteria criteria = new Criteria();
		criteria.and("enInID").is(ncTempPayInfo.getEnInID());
		criteria.and("supplyID").is(ncTempPayInfo.getSupplyID());
		Query query = new Query();
		query.addCriteria(criteria);
		_log.info(query.toString());
		Update update = new Update();
		update.set("orderSource", ncTempPayInfo.getOrderSource());
		update.set("update_time", new Date());
		mongoTemplate.updateFirst(query, update, MongodbConstant.TABLENAME_CHECKINOUT);
		mongoTemplate.updateFirst(query, update, MongodbConstant.TABLENAME_CHECKOUT);

	}

	@Override
	public void updateLineTempPay(ApAppearanceRecord apAppearanceRecord) {
		String temppay_tablename = MongodbConstant.TABLENAME_TEMPPAY;
		Criteria criteria = new Criteria();
		criteria.and("orderId").is(apAppearanceRecord.getPayOrderID());
		Query query = new Query();
		query.addCriteria(criteria);
		_log.info(query.toString());
		Update update = new Update();
		update.set("opType", apAppearanceRecord.getOpType());
		update.set("opSpecial", apAppearanceRecord.getOpSpecial());
		update.set("cardType", apAppearanceRecord.getCardType());
		update.set("carType", apAppearanceRecord.getCarType());
		update.set("carType", apAppearanceRecord.getCarType());
		update.set("supplyID", apAppearanceRecord.getSupplyID());
		update.set("cardCode", apAppearanceRecord.getCardCode());
		update.set("parkCode", apAppearanceRecord.getParkId());
		
		update.set("enInID", apAppearanceRecord.getEnInID());
		update.set("enterTime", apAppearanceRecord.getEnterTime());
		update.set("roomName", apAppearanceRecord.getRoomName());
		update.set("enOutID", apAppearanceRecord.getEnOutID());
		update.set("outTime", apAppearanceRecord.getOutTime());
		update.set("outRoomName", apAppearanceRecord.getOutRoomName());
		update.set("outRoomName", apAppearanceRecord.getOutRoomName());
		update.set("update_time", new Date());
		mongoTemplate.updateFirst(query, update, temppay_tablename);
	}

	@Override
	public void updateInoutcheck(ApAppearanceRecord apAppearanceRecord) {
		Criteria criteria = new Criteria();
		criteria.and("enInID").is(apAppearanceRecord.getEnInID());
		criteria.and("feeMoney").is(0);
		criteria.and("supplyID").is(apAppearanceRecord.getSupplyID());
		Query query = new Query();
		query.addCriteria(criteria);
		_log.info(query.toString());
		Update update = new Update();
		update.set("shouldFeeMoney", apAppearanceRecord.getShouldFeeMoney());
		update.set("feeMoney", apAppearanceRecord.getFeeMoney());
		update.set("update_time", new Date());
		mongoTemplate.updateFirst(query, update, MongodbConstant.TABLENAME_CHECKINOUT);
		mongoTemplate.updateFirst(query, update, MongodbConstant.TABLENAME_CHECKOUT);
	}
}
